﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace AlwaysOnTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txtReadonly.Text = "select @@servername";
            txtWrite.Text = "update myuser set ModifiedTime=ModifiedTime where UserCode='admin'";
            lblCurTime.Text = ((DateTime.Now.Ticks - (new DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 10000000).ToString();
            lblCurTime.Text += "|" + ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
            lblCurTime.Text += "|" + ((DateTime.Now.ToUniversalTime().Ticks - (new DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 10000000).ToString();
        }

        private void setMasterDbConnStr()
        {
            string masterDbConnStr = $"Data Source={txtDbServer.Text};Initial Catalog={txtDbName.Text};User ID={txtUserName.Text};Password={txtPassword.Text}";
            if (radsalver.Checked)
            {
                masterDbConnStr = masterDbConnStr.TrimEnd(';') + ";ApplicationIntent=ReadOnly;MultiSubnetFailover=True";
            }
            txtConn.Text = masterDbConnStr;
        }

        private void radmaster_CheckedChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }

        private void radsalver_CheckedChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                using (SqlConnection conn = new SqlConnection(txtConn.Text))
                {
                    SqlCommand com = new SqlCommand();
                    com.Connection = conn;
                    com.CommandText = txtReadonly.Text;
                    conn.Open();
                    string result = com.ExecuteScalar().ToString();
                    conn.Close();
                    MessageBox.Show($"SQL执行结果：" + result);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(txtConn.Text))
                {
                    SqlCommand com = new SqlCommand();
                    com.Connection = conn;
                    com.CommandText = txtWrite.Text;
                    conn.Open();
                    string result = com.ExecuteNonQuery().ToString();
                    conn.Close();
                    MessageBox.Show($"SQL执行受影响行数：" + result);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtDbServer_TextChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }

        private void txtDbName_TextChanged(object sender, EventArgs e)
        {
            setMasterDbConnStr();
        }
    }
}
